package com.example.a10.server;

import com.example.a10.util.AddressConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.json.JsonParser;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;

@Service
public class JsonServer {

    @Autowired
    private JsonParser jsonParser;

    /**
     * 读取Json文件数据
     * @param resultname
     * @return
     * @throws IOException
     */
    public Map<String, Object> readJsonFile(String resultname) throws IOException {
        Path path = Paths.get(AddressConstants.Result+resultname).toAbsolutePath();
        byte[] bytes = Files.readAllBytes(path);
        String jsonContent = new String(bytes);
        return jsonParser.parseMap(jsonContent);
    }

    public Map<String, Object> readfeatureJsonFile(String resultname) throws IOException {
        Path path = Paths.get(AddressConstants.Feature+resultname).toAbsolutePath();
        byte[] bytes = Files.readAllBytes(path);
        String jsonContent = new String(bytes);
        return jsonParser.parseMap(jsonContent);
    }

}
